﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Idp_Login.DbModel;
using Microsoft.EntityFrameworkCore;

namespace Idp_Login.Service
{
    public interface IAdminService
    {
        Task<Admin> GetByUserNameAndPwd(string username, string pwd);//根据用户名和密码查找用户
        Task<Admin> GetById(int id);
    }

    public class AdminService : IAdminService
    {
        private IdpContext db;
        public AdminService(IdpContext idpContext)
        {
            db = idpContext;
        }
        /// <summary>
        /// 验证用户，成功则返回用户信息，否则返回null
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task<Admin> GetByUserNameAndPwd(string username, string pwd)
        {
            Admin m = await db.Admin.Where(a => a.UserName == username && a.Password == pwd).SingleOrDefaultAsync();
            return m ?? null;
        }

        public async Task<Admin> GetById(int id)
        {
            Admin m = await db.Admin.Where(a => a.Id == id).SingleOrDefaultAsync();
            return m ?? null;
        }
    }
}
